﻿using _8.test;
using DevExpress.Utils;
using DevExpress.Utils.Drawing;
using DevExpress.Utils.Extensions;
using DevExpress.XtraSplashScreen;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Handlers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using XCI.Core;
using XCI.Extensions;
using XCI.Helper;
using XCI.Sys;
using XCI.Sys.Service;
using XCI.Windows.Core;
using XCI.Windows.Forms;
using XCI.Windows.Helper;

namespace XCI.Test
{
    public partial class Form2 : FrmBase
    {
        OverlayTextPainter overlayLabel;

        public Form2()
        {
            InitializeComponent();
            this.overlayLabel = new OverlayTextPainterEx();
            hyperlinkLabelControl1.Parent = pictureEdit1;
            hyperlinkLabelControl1.Location = new Point(100, 100);
        }

        private void PictureEdit1_PopupMenuShowing(object sender, DevExpress.XtraEditors.Events.PopupMenuShowingEventArgs e)
        {
            e.PopupMenu.Items[4].Caption = "选择文件";
            e.PopupMenu.Items[7].Visible = false;
        }

        private void HyperlinkLabelControl1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            pictureEdit1.LoadAsync(openFileDialog1.FileName);
            hyperlinkLabelControl1.Visible = false;
        }

        private void SimpleButton1_Click(object sender, EventArgs e)
        {
            //自动生成文件夹和文件名
            //var fileInfo1 = FileUploadService.Instance.UploadFile(null, null, "风景.jpg", "R:\\5.jpg");
            // MessageBoxHelper.ShowMessage(JsonHelper.Serialize(fileInfo1, Formatting.Indented));


            ////var handle = SplashScreenManager.ShowOverlayForm(pictureEdit1, 
            ////    customPainter: new CustomOverlayPainter(pictureEdit1.ClientRectangle));

            //var handle = SplashScreenManager.ShowOverlayForm(pictureEdit1,
            //    //backColor: Color.FromArgb(128, 51, 51, 51),
            //    opacity: 200,
            //    customPainter: new OverlayWindowCompositePainter(overlayLabel));
            var overform = new XCIOverlayForm();
            overform.Show(pictureEdit1);
            Task.Run(() =>
            {
                for (int j = 0; j < 10000; j++)
                {
                    overform.SetText("正在上传... " + j.ToString() + "%");
                    Thread.Sleep(60);

                    if (j==60)
                    {
                        overform.SetFont(new Font("微软雅黑",14f));
                        overform.SetColor(Color.Red);
                    }
                    else if (j == 120)
                    {
                        overform.SetFont(new Font("微软雅黑", 18f));
                        overform.SetColor(Color.Green);
                    }
                }
            });

            Task.Delay(10000).ContinueWith(p => { overform.Close(); },
                TaskScheduler.FromCurrentSynchronizationContext());
        }

        private void SimpleButton2_Click(object sender, EventArgs e)
        {
            var handle = SplashScreenManager.ShowOverlayForm(pictureEdit1,
                 //backColor: Color.FromArgb(128, 51, 51, 51),
                 opacity: 200,
                 customPainter: new OverlayWindowCompositePainter(overlayLabel));
            Image img = null;
            Task.Run(() =>
            {
                var serverUrl = "/res/uploads/1117664844619845632/20190802/a.jpg";
                byte[] fileBytes = FileUploadService.Instance.Download(serverUrl, null);
                img = ImageHelper.FromArray(fileBytes);
            }).ContinueWith(p =>
            {
                pictureEdit1.Image = img;
                SplashScreenManager.CloseOverlayForm(handle);
            },
                TaskScheduler.FromCurrentSynchronizationContext());
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            var fileInfo1 = FileUploadService.Instance.UploadFile(null, null, "r.rar", "R:/abc.rar");
            MessageBoxHelper.ShowMessage(JsonHelper.Serialize(fileInfo1));
            button1.Enabled = true;
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            var surl = "res/uploads/1117664844619845632/20190804/1d6a79ac6ba14d6bbc2ef97916f720f1.rar";
            var url = "/api/sys/fileUpload/download";
            var map = Map.New.Add("path", surl);

            Task.Run(() =>
            {
                var result = SysRuntime.Api.DownloadPost(url, map, "r:/abc.rar", p =>
                 {
                     ControlInvoke(progressBarControl1, () =>
                     {
                         progressBarControl1.Position = p.ProgressPercentage;
                     });
                 });
                if (result.Msg.IsNotEmpty())
                {
                    MessageBox.Show(result.Msg.ToString());
                }
            });
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            var surl = "res/uploads/1117664844619845632/20190804/1d6a79ac6ba14d6bbc2ef97916f720f1.rar";
            Task.Run(() =>
            {
                FileUploadService.Instance.Download(surl, "r:/abc.rar", null, p =>
                  {
                      ControlInvoke(progressBarControl1, () =>
                      {
                          progressBarControl1.Position = p.ProgressPercentage;
                      });
                  });

                //var result = FileUploadService.Instance.Download(surl, "r:/abc.rar", null);
                //MessageBox.Show(result.Success.ToString());
            });
        }

        private void Button4_Click(object sender, EventArgs e)
        {
            Task.Run(() =>
            {
                FileUploadService.Instance.UploadFile(null, "456.rar", "r.rar", "R:/abc.rar", p =>
                {
                    ControlInvoke(progressBarControl1, () =>
                    {
                        progressBarControl1.Position = p.ProgressPercentage;
                    });
                });
            });
        }
    }

    class OverlayTextPainterEx : OverlayTextPainter
    {
        public OverlayTextPainterEx()
        {
            this.Font = new Font("微软雅黑", 14);
            //this.Color = Color.White;
        }

        protected override void DrawText(GraphicsCache cache)
        {
            var f = AppearanceObject.ControlAppearance.GetStringFormat();
            f.Alignment = StringAlignment.Center;
            cache.DrawString(this.Text, this.GetFont(), cache.GetSolidBrush(this.Color), this.Bounds, f);
        }

        protected override Rectangle CalcTextBounds(OverlayLayeredWindowObjectInfoArgs drawArgs)
        {
            //return drawArgs.Bounds;
            var rect = drawArgs.Bounds;
            return new Rectangle(rect.X, rect.Y + drawArgs.ImageBounds.Height, rect.Width, rect.Height);
            //textSz.AlignWith(drawArgs.Bounds).WithY(drawArgs.ImageBounds.Top - textSz.Height);
        }
    }

}
